--beike.lua www.bigiot.net

-- from www.bigiot.net get 121.42.180.30
local HOST = "www.bigiot.net";
local PORT = 8181;
local DEVICEID = "781";
local APIKEY = "e9701c2d6";
local INPUTID = "733";

local function run() 

    local client = net.createConnection(net.TCP);
    client:connect(PORT, HOST);
    ok, s = pcall(cjson.encode, {M="checkin",ID=DEVICEID,K=APIKEY});
    if (ok) then
        
        client:send(s .. "\n");
        -- heart keep 
        tmr.alarm(1, 50000, 1, function () 
           client:send(s .. "\n"); 
        end);
    else
        print("faied to endcode");
    end
    
    -- recv
    client:on("receive", function(socket, data)
        print("receive from server : " .. data);
    end);

    client:on("disconnection", function(socket)
        client = nil;
        tmr.stop(1);
        -- 5s retry
        tmr.alarm(0, 5000, 0, run);
    end);
    
end

run();





